/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.rest;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.biz.domain.Lab;
import me.zhengjie.modules.biz.service.LabService;
import me.zhengjie.modules.biz.service.dto.LabQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author Admin
* @date 2023-02-17
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "lab管理")
@RequestMapping("/api/lab")
public class LabController {

    private final LabService labService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('lab:list')")
    public void exportLab(HttpServletResponse response, LabQueryCriteria criteria) throws IOException {
        labService.download(labService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询lab")
    @ApiOperation("查询lab")
    @AnonymousAccess
    public ResponseEntity<Object> queryLab(LabQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(labService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增lab")
    @ApiOperation("新增lab")
//    @PreAuthorize("@el.check('lab:add')")
    public ResponseEntity<Object> createLab(@Validated @RequestBody Lab resources){
        return new ResponseEntity<>(labService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改lab")
    @ApiOperation("修改lab")
//    @PreAuthorize("@el.check('lab:edit')")
    public ResponseEntity<Object> updateLab(@Validated @RequestBody Lab resources){
        labService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除lab")
    @ApiOperation("删除lab")
//    @PreAuthorize("@el.check('lab:del')")
    public ResponseEntity<Object> deleteLab(@RequestBody Integer[] ids) {
        labService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/3d/lab-all")
    @Log("查询所有lab信息")
    @ApiOperation("查询所有lab信息")
    @AnonymousAccess
    public ResponseEntity<Object> queryLabAll(){
        return new ResponseEntity<>(labService.queryLabAll(),HttpStatus.OK);
    }

    @GetMapping("/3d/lab-get")
    @Log("查询单个lab")
    @ApiOperation("查询单个lab")
    @AnonymousAccess
    public ResponseEntity<Object> queryLabById(@RequestParam String roomNumber){
        return new ResponseEntity<>(labService.queryLabByRoomNumber(roomNumber),HttpStatus.OK);
    }

    @GetMapping("/3d/floor-info")
    @Log("查询某一层lab")
    @ApiOperation("查询某一层lab")
    @AnonymousAccess
    public ResponseEntity<Object> queryLabByFloor(@RequestParam Integer floor){

        return new ResponseEntity<>(labService.queryLabByFloor(floor),HttpStatus.OK);
    }

    @GetMapping("/3d/leftAll")
    @Log("查询数据分析侧边栏信息")
    @ApiOperation("查询数据分析侧边栏信息")
    @AnonymousAccess
    public ResponseEntity<Object> queryLeftAll(){

        return new ResponseEntity<>(labService.queryLeftAll(),HttpStatus.OK);
    }

    @GetMapping("/findAdmin")
    @Log("查询实验室管理员列表")
    @ApiOperation("查询实验室管理员列表")
    @AnonymousAccess
    public ResponseEntity<Object> queryAdminAll(){

        return new ResponseEntity<>(labService.queryAdminAll(),HttpStatus.OK);
    }

    @GetMapping("/3d/line-info")
    @Log("查询具体折线图信息")
    @ApiOperation("查询具体折线图信息")
    @AnonymousAccess
    public ResponseEntity<Object> queryLineInfo(@RequestParam Integer id, @RequestParam Integer type, @RequestParam String startTime, @RequestParam String endTime){
        if (type.equals(1)) {
            return null;
        } else if (type.equals(2)) {
            return new ResponseEntity<>(labService.queryLineInfo(id, Convert.toInt(Convert.toLong(startTime) / 1000), Convert.toInt(Convert.toLong(endTime) / 1000)),HttpStatus.OK);
        }
        return null;
    }

    @GetMapping("/cabinetList")
    @ApiOperation("包含试剂柜的实验室列表")
    public ResponseEntity<Object> cabinetList(){
        return new ResponseEntity<>(labService.cabinetList(),HttpStatus.OK);
    }

    @GetMapping("/findLabAll")
    @Log("查询所有lab")
    @ApiOperation("查询所有lab")
    @AnonymousAccess
    public ResponseEntity<Object> queryLabAll(LabQueryCriteria criteria){
        return new ResponseEntity<>(labService.queryAll(criteria),HttpStatus.OK);
    }

}
